﻿<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html"/>

  <xsl:param name="details">false</xsl:param>
  <xsl:param name="indexTableCols">3</xsl:param>
  <xsl:param name="thumbShowCaption">true</xsl:param>
  <xsl:param name="pageTitle">Movie Collection</xsl:param>
  <xsl:param name="includeCreationDate">true</xsl:param>

  <!-- the main template -->
  <xsl:template match="/">
    <HTML>
      <HEAD>
        <META http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <TITLE>
          <xsl:value-of select="$pageTitle"/>
        </TITLE>
      </HEAD>
      <STYLE>
	    <EXPORTSTYLE/>
      </STYLE>
      <BODY>
        <div align="center">
          <span class="title">
            <xsl:value-of select="$pageTitle"/>
          </span>
          <xsl:apply-templates select="movies/navigation"/>
          <br/>
          <table>
            <xsl:apply-templates/>
          </table>
          <xsl:if test="$includeCreationDate='true'">
            <br/>
            <div class="value">
              <xsl:value-of select="//@creationDate"/>
            </div>
          </xsl:if>
        </div>
      </BODY>
    </HTML>
  </xsl:template>

  <xsl:template match="movies">
    <xsl:for-each select="movieInfo[position() mod $indexTableCols = 1]">
      <tr>
        <xsl:apply-templates select=". | following-sibling::movieInfo[position() &lt; $indexTableCols]"/>
      </tr>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="movieInfo">
    <td valign="top">
      <xsl:choose>
        <xsl:when test="$details = 'true'">
          <a href="details/{id}.html" title="{title}" id="thumbimage">
            <img src="images/{id}t.jpg"/>
          </a>
        </xsl:when>
        <xsl:otherwise>
          <a href="images/{id}.jpg" title="{title}" id="thumbimage">
            <img src="images/{id}t.jpg"/>
          </a>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:if test="$thumbShowCaption = 'true'">
        <br/>
        <xsl:choose>
          <xsl:when test="$details = 'true'">
            <a href="details/{id}.html" title="{title}">
              <xsl:value-of select="title"/>
            </a>
          </xsl:when>
          <xsl:otherwise>
            <a href="images/{id}.jpg" title="{title}">
              <xsl:value-of select="title"/>
            </a>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>
    </td>

    <xsl:if test="(last() &gt; $indexTableCols) and (last()=position()) and (position() mod $indexTableCols &gt; 0)">
      <xsl:variable name="filledCells" select="position() mod $indexTableCols"/>
      <td colspan="{$indexTableCols - $filledCells}">&#160;</td>
    </xsl:if>
  </xsl:template>

  <xsl:template match="navigation">
    <div class="navigation" align="center">
      <xsl:if test="count(pagelink) > 1">
        <div class="navigationline">
          <xsl:for-each select="pagelink">
            <xsl:choose>
              <xsl:when test="@url!=''">
                <div class="navlink">
                  <a href="{@url}">
                    <xsl:value-of select="@pagenum"/>
                  </a>
                </div>
              </xsl:when>
              <xsl:otherwise>
                <div class="navlink" id="current">
                  <xsl:value-of select="@pagenum"/>
                </div>
              </xsl:otherwise>
            </xsl:choose>
            <xsl:if test="position()!=last()"></xsl:if>
          </xsl:for-each>
        </div>
        <div class="navigationline">
          <xsl:choose>
            <xsl:when test="firstlink/@url!=''">
              <div class="navlink">
                <a href="{firstlink/@url}">
                  <xsl:value-of select="/movies/metadata/field[@name='first']/@label"/>
                </a>
              </div>
            </xsl:when>
            <xsl:otherwise>
              <div class="navlink">
                <xsl:value-of select="/movies/metadata/field[@name='first']/@label"/>
              </div>
            </xsl:otherwise>
          </xsl:choose>
          <xsl:choose>
            <xsl:when test="prevlink/@url!=''">
              <div class="navlink">
                <a href="{prevlink/@url}">
                  <xsl:value-of select="/movies/metadata/field[@name='prev']/@label"/>
                </a>
              </div>
            </xsl:when>
            <xsl:otherwise>
              <div class="navlink">
                <xsl:value-of select="/movies/metadata/field[@name='prev']/@label"/>
              </div>
            </xsl:otherwise>
          </xsl:choose>
          <xsl:choose>
            <xsl:when test="nextlink/@url!=''">
              <div class="navlink">
                <a href="{nextlink/@url}">
                  <xsl:value-of select="/movies/metadata/field[@name='next']/@label"/>
                </a>
              </div>
            </xsl:when>
            <xsl:otherwise>
              <div class="navlink">
                <xsl:value-of select="/movies/metadata/field[@name='next']/@label"/>
              </div>
            </xsl:otherwise>
          </xsl:choose>
          <xsl:choose>
            <xsl:when test="lastlink/@url!=''">
              <div class="navlink">
                <a href="{lastlink/@url}">
                  <xsl:value-of select="/movies/metadata/field[@name='last']/@label"/>
                </a>
              </div>
            </xsl:when>
            <xsl:otherwise>
              <div class="navlink">
                <xsl:value-of select="/movies/metadata/field[@name='last']/@label"/>
              </div>
            </xsl:otherwise>
          </xsl:choose>
        </div>
      </xsl:if>
    </div>
  </xsl:template>

</xsl:stylesheet>

